#include "OnGroundState.h"
#include "ShareComponents.h"
#include "Player.h"
#include <math.h>
#include "FWKeyboard.h"
#define CONTRA_VX 2.4f 
#define CONTRA_VY 8.5f
#include "RSManager.h"


OnGroundState::OnGroundState(Player* player)
{//
	p = player;

	gDirection = RIGHT;
	jDirection = UP;

	this->_sGoLeft = RSManager::getInstance()->getSprite(PLAYER_GO_LEFT);
	this->_sGoRight = RSManager::getInstance()->getSprite(PLAYER_GO_RIGHT);

	this->_sIII = RSManager::getInstance()->getSprite(PLAYER_III);
	this->_sIV = RSManager::getInstance()->getSprite(PLAYER_IV);
	this->_sII = RSManager::getInstance()->getSprite(PLAYER_II);
	this->_sI = RSManager::getInstance()->getSprite(PLAYER_I);

	this->_sLieLeft = RSManager::getInstance()->getSprite(PLAYER_LIE_LEFT);
	this->_sLieRight = RSManager::getInstance()->getSprite(PLAYER_LIE_RIGHT);
	this->_sLieShootLeft = RSManager::getInstance()->getSprite(PLAYER_LIE_SHOOT_LEFT);
	this->_sLieShootRight = RSManager::getInstance()->getSprite(PLAYER_LIE_SHOOT_RIGHT);

	this->_sShootLeft = RSManager::getInstance()->getSprite(PLAYER_SHOOT_LEFT);
	this->_sShootRight = RSManager::getInstance()->getSprite(PLAYER_SHOOT_RIGHT);
	this->_sStandLeft = RSManager::getInstance()->getSprite(PLAYER_STAND_LEFT);
	this->_sStandRight = RSManager::getInstance()->getSprite(PLAYER_STAND_RIGHT);

	this->_sShootTopLeft = RSManager::getInstance()->getSprite(PLAYER_SHOOT_TOP_LEFT);
	this->_sShootTopRight = RSManager::getInstance()->getSprite(PLAYER_SHOOT_TOP_RIGHT);
	this->_sStandTopLeft = RSManager::getInstance()->getSprite(PLAYER_STAND_TOP_LEFT);
	this->_sStandTopRight = RSManager::getInstance()->getSprite(PLAYER_STAND_TOP_RIGHT);

	this->_sGoShootLeft = RSManager::getInstance()->getSprite(PLAYER_GSHOOT_LEFT);
	this->_sGoShootRight = RSManager::getInstance()->getSprite(PLAYER_GSHOOT_RIGHT);

	p->setSprite(_sStandRight);
	_isJumping = false;
	timeDelay = 180;
}

OnGroundState::~OnGroundState(void)
{
}

PLAYER_STATE OnGroundState::handleInput()
{
	KBState = 0;	
	p->set_vx(0);
	if (gl_keyboard->isKeyDown(DIK_D))	
		KBState = KBState | D;					
	if (gl_keyboard->isKeyDown(DIK_W))
		KBState = KBState | W;
	if (gl_keyboard->isKeyDown(DIK_A))	
		KBState = KBState | A;					
	if (gl_keyboard->isKeyDown(DIK_S))	
		KBState = KBState | S;					
	if (gl_keyboard->isKeyPressed(DIK_U))
		KBState = KBState | _U;
	if(p->getBulletID() == B_M)
	{
		if(gl_keyboard->isKeyDown(DIK_I))		
			KBState = KBState | I;		
	}
	else
	{
		if(gl_keyboard->isKeyPressed(DIK_I))		
			KBState = KBState | I;			
	}

	switch (KBState)
	{
	case A:
		p->set_vx(-CONTRA_VX);
		if(!setDelay(GAME_TIME))
			p->setSprite(_sGoLeft);
		gDirection = LEFT;
		break;
	case D:
		p->set_vx(CONTRA_VX);
		if(!setDelay(GAME_TIME))
			p->setSprite(_sGoRight);
		gDirection = RIGHT;
		break;
	case W:
		if(gDirection == LEFT)
			p->setSprite(_sStandTopLeft);
		else
			p->setSprite(_sStandTopRight);
		break;
	case S:
		if(gDirection == LEFT)
			p->setSprite(_sLieLeft);
		else
			p->setSprite(_sLieRight);
		break;
	case _U:
		_isJumping = true;
		jDirection = UP;
		return ONSPACE;
	case I:
		if(gDirection == LEFT)			
		{
			p->setSprite(_sShootLeft);
			p->shoot(0, 38, M_PI);
		}
		else
		{
			p->setSprite(_sShootRight);
			p->shoot(40, 38, 0);	
		}
		break;
	case AS:
		p->setSprite(_sIII);
		p->set_vx(-CONTRA_VX);
		gDirection = LEFT;
		break;
	case AW:
		p->setSprite(_sII);
		p->set_vx(-CONTRA_VX);
		gDirection = LEFT;
		break;
	case DW:
		p->setSprite(_sI);
		p->set_vx(CONTRA_VX);
		gDirection = RIGHT;
		break;
	case DS:
		p->setSprite(_sIV);
		p->set_vx(CONTRA_VX);
		gDirection = RIGHT;
		break;
	case DI:
		p->set_vx(CONTRA_VX);		
		p->setSprite(_sGoShootRight);
		p->shoot(40, 38, 0);
		gDirection = RIGHT;
		timeDelay = 0;
		break;
	case AI:
		p->set_vx(-CONTRA_VX);
		p->setSprite(_sGoShootLeft);
		p->shoot(0, 38, M_PI);
		gDirection = RIGHT;
		timeDelay = 0;
		break;
	case WI:
		if(gDirection == LEFT)
		{
			p->setSprite(_sShootTopLeft);
			p->shoot(23, 90, M_PI_2);
		}
		else
		{
			p->setSprite(_sShootTopRight);
			p->shoot(20, 90, M_PI_2);
		}
		break;
	case SI:
		if(gDirection == LEFT)
		{
			p->setSprite(_sLieShootLeft);
			p->shoot(0, 15, M_PI);
		}
		else
		{
			p->setSprite(_sLieShootRight);
			p->shoot(47, 15, 0);
		}
		break;
	case DU:				
		_isJumping = true;
		jDirection = UP;
		return ONSPACE;
		break;
	case SU:		
		_isJumping = true;
		jDirection = DOWN;
		return ONSPACE;
		break;
	case AU:
		_isJumping = true;
		jDirection = UP;
		return ONSPACE;
		break;
	case WU:
		_isJumping = true;
		jDirection = UP;
		return ONSPACE;
		break;
	case ASU:
		_isJumping = true;
		jDirection = UP;
		return ONSPACE;
		break;
	case DSU:
		_isJumping = true;
		jDirection = UP;
		return ONSPACE;
		break;
	case DWU:		
		_isJumping = true;
		jDirection = UP;
		return ONSPACE;
		break;
	case AWU:
		_isJumping = true;
		jDirection = UP;
		return ONSPACE;
		break;
	case ASI:
		p->setSprite(_sIII);
		p->shoot(0, 24, 5*M_PI/4);
		break;
	case DSI:
		p->setSprite(_sIV);
		p->shoot(30, 28, -M_PI/4);
		break;
	case DWI:
		p->setSprite(_sI);
		p->shoot(30, 60, M_PI/4);
		break;
	case AWI:
		p->setSprite(_sII);
		p->shoot(0, 60, 3*M_PI/4);
		break;	
	default:
		if(gDirection == LEFT)
			p->setSprite(_sStandLeft);
		else
			p->setSprite(_sStandRight);
		break;
	}
	return ONGROUND;
}

void OnGroundState::setJumpDirection(JUMP_DIRECTION j)
{
	jDirection = j;
}
